/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { PureComponent } from 'react';
import { Select, Spin } from 'antd';

const { Option: SelectOption } = Select;

class StandardSelect extends PureComponent {
  render() {
    const {
      labelInValue,
      options,
      kkey = 'name',
      loading,
      notFoundContent = '暂无数据',
      value,
      ...restProps
    } = this.props;
    let loadingValue;
    // 生成下拉选项
    const Options =
      options &&
      options.map(item => {
        if (item) {
          const { name, code, sby } = item;
          // code模式加载中，显示数据强制改为name
          if (!labelInValue && kkey === 'code') {
            if (loading && code === value) {
              loadingValue = name;
            }
          } else {
            loadingValue = value;
          }
          return (
            <SelectOption sby={sby || `${name}-${code}`} key={labelInValue ? code : item[kkey]}>
              {name}
            </SelectOption>
          );
        }
      });
    return (
      <Select
        value={loading ? loadingValue : value}
        style={{ width: 150 }}
        labelInValue={labelInValue}
        optionFilterProp="sby"
        notFoundContent={loading ? <Spin spinning={true} size="small" /> : notFoundContent}
        {...restProps}
      >
        {loading ? null : Options}
      </Select>
    );
  }
}

export default StandardSelect;
